<?php

//query to fill up the drop-down box
$username="bsc353_4";
$password="sonbeans";
$database="bsc353_4";

mysql_connect("clipper.encs.concordia.ca",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT * FROM Customer;";
$result = mysql_query($query) or die ("Query unsuccessful");


//populate the fields. If the page posts back to itself, these will be filled
if(isset($_POST['selection']))
{
    $ID =  $_POST['selection'];

    $queryFill = "SELECT * FROM Customer WHERE ID = '$ID';";
  
    $resultFill = mysql_query($queryFill) or die ("Failed to fill fields from database");

    $i=0;
    $num=mysql_numrows($resultFill);
    
    $name= mysql_result($resultFill, 0, "name");
    $address=mysql_result($resultFill, 0, "address");
    $city=mysql_result($resultFill, 0, "city");
    $province=mysql_result($resultFill, 0, "province");
    $postalCode = mysql_result($resultFill, 0, "postalCode");
    $phoneNumber=mysql_result($resultFill, 0, "phoneNumber");
    $numPurchases= mysql_result($resultFill, 0, "numPurchases");                
}



?>



<html>
<head>
<title>Edit Customer Data</title>
<style type="text/css">
body {
	font-family:verdana,arial,sans-serif;
	font-size:10pt;
	margin:30px;
	background-color:#ffcc00;
	}
</style>
</head>
<body>
<h1>Edit Customer Data</h1>
<form name = "chooseCustomer" action="content.php" method = "post">
<select name = "selection" onChange = "onChangeSelect"> 
    <?php
    $i=0;
    $num=mysql_numrows($result);
    while ($i < $num) 
    {   
        $varName = mysql_result($result, $i, "name");
        $varID = mysql_result($result, $i, "ID");
        echo  "<option value='$varID'";
        if(isset($_POST['selection']) && $varID == $ID)
        {
            echo "selected = 'selected'";
        }    
	echo  ">$varName </option>";
             
        $i++; 
    }
    ?>
</select>
<input type="Submit" value = "Select Customer">
</form>

<form name= "editCustomer" action="editcustomer_update.php" method="post">



<br>

<table>

<?php
if(isset($_POST['selection'])) //set the fields based on the book selection
{
    echo "<tr><td style='text-align:right'>ID: </td><td><input type='text' name='ID' value = '$ID'></td></tr>";
    echo "<tr><td style='text-align:right'>Name: </td><td><input type='text' name='name' value = '$name'></td></tr>";
    echo "<tr><td style='text-align:right'>Address: </td><td><input type='text' name='address' value = '$address'></td></tr>";
    echo "<tr><td style='text-align:right'>City: </td><td><input type='text' name='city' value = '$city'></td></tr>";
    echo "<tr><td style='text-align:right'>Province: </td><td><input type='text' name='province' value = '$province'></td></tr>";
    echo "<tr><td style='text-align:right'>Postal Code: </td><td><input type='text' name='postalCode' value = '$postalCode'></td></tr>";
    echo "<tr><td style='text-align:right'>Phone Number: </td><td><input type='text' name='phoneNumber' value = '$phoneNumber'></td></tr>";
    echo "<tr><td style='text-align:right'>Number of purchases: </td><td><input type='text' name='numPurchases' value = '$numPurchases'></td></tr>";
    echo "<input type='hidden' name='origID' value='$ID' />";
}
else
{
    echo "<tr><td style='text-align:right'>ID: </td><td><input type='text' name='ID' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>Name: </td><td><input type='text' name='name' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>Address: </td><td><input type='text' name='address' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>City: </td><td><input type='text' name='city' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>Province: </td><td><input type='text' name='province' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>Postal Code: </td><td><input type='text' name='postalCode' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>Phone Number: </td><td><input type='text' name='phoneNumber' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>Number of purchases: </td><td><input type='text' name='numPurchases' value = ''></td></tr>";
}
?>

<tr><td style='text-align:center'><input type="Submit" name = "delete" value = "Delete"></td>
<td style='text-align:center'><input type="Submit" name = "save" value = "Save Changes"></td></tr>
</table>


</form> 


</body>
</html>